package com.mtr.databoard.domain.resources;

import com.mtr.databoard.common.MD5Util;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.UUID;

@Component
public class ResourceManager {

    private ResourceStore resourceStore;

    private IResourceRepository resourceRepository;

    private IFileContentRepository fileContentRepository;

    public ResourceManager(@Autowired ResourceStore resourceStore, @Autowired IResourceRepository resourceRepository, @Autowired IFileContentRepository fileContentRepository) {
        this.resourceStore = resourceStore;
        this.resourceRepository = resourceRepository;
        this.fileContentRepository = fileContentRepository;
    }

    /**
     * 保存资源
     *
     * @param fileName
     * @param fileType
     * @param content
     * @return
     * @throws IOException
     * @throws NoSuchAlgorithmException
     */
    public Resource saveResource(String fileName, String fileType, Binary content) throws IOException, NoSuchAlgorithmException {
        String fileId = MD5Util.getMD5(content.getData());

        Resource resource = null;

        if (!fileContentRepository.existsById(fileId)) {
            fileContentRepository.insert(new FileContent(fileId, content));
        }

        if (resourceRepository.existsById(fileId)) {
            return resourceRepository.findById(fileId).get();
        } else {
            resource = new Resource(fileId, fileName, fileType, content.length());
            resourceRepository.insert(resource);
            return resource;
        }
    }

    /**
     * 获取资源
     *
     * @param fileId
     * @return
     */
    public Resource getResource(String fileId) {
        return  resourceStore.getResource(fileId);
    }

    /**
     * 获取文件内容
     *
     * @param fileId
     * @return
     */
    public FileContent getFileContent(String fileId) {
        return resourceStore.getFileContent(fileId);
    }

    /**
     * 绑定实体
     *
     * @param fileId
     * @param entity
     */
    public void bindEntity(String fileId, ResourceEntity entity) {
        resourceStore.bindEntity(fileId,entity);
    }

    /**
     * 解绑实体
     *
     * @param fileId
     * @param entity
     */
    public void unbindEntity(String fileId, ResourceEntity entity) {
        resourceStore.unbindEntity(fileId,entity);
    }


    /**
     * 清空无实体关联的数据
     */
    public void clearEmptyResources() {
        resourceStore.clearEmptyResources();
    }

    /**
     * 解压资源到临时文件夹（从数据库中将二进制存为文件）
     *
     * @param basePath
     * @param contextId
     * @return 返回文件目录
     */
    public String releaseResources(List<ContextResource>resources,String basePath, String contextId, boolean isRebuild) throws IOException {
        return  resourceStore.releaseResources(resources,basePath,contextId,isRebuild);
    }

    /**
     * 获取文件资源
     *
     * @param basePath
     * @param contextId
     * @param filePath
     * @return
     */
    public Binary getFileContent(String basePath,String contextId, String filePath){
        return  resourceStore.getFileContent(basePath,contextId,filePath);
    }

    /**
     * 清除资源
     * @param basePath
     * @param contextId
     */
    public void clearReleasedResources(String basePath, String contextId){
        resourceStore.clearReleasedResources(basePath,contextId);
    }

    /**
     * 获取文件内容
     * @param resourceId 资源id
     * @param filePath 文件名，可带目录
     * @return
     * @throws IOException
     */
    public Binary getFileContent(String resourceId, String filePath) throws IOException
    {
        return  resourceStore.getFileContent(resourceId,filePath);
    }
}
